﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.EducationAndTraining
{
    public class SuccessStatuses : DetailView<successstatu>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<successstatu> EntityList;
        private new successstatu Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<successstatu>

        public override List<successstatu> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.successstatus.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<successstatu> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.successstatus.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override successstatu GetEntity()
        {
            _context = new project5188Entities();
            Entity = new successstatu();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTosuccessstatus(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        public List<successstatu> GetEntityListByParameters(string candidateNoText, string studentNameSurname, string coursePeriod, string successStatus, string teacherNameSurname, string termNoText)
        {
            _context = new project5188Entities();

            int candidateNo = 0, termNo = 0;

            if (!String.IsNullOrEmpty(candidateNoText) && !String.IsNullOrWhiteSpace(candidateNoText))
            {
                Int32.TryParse(candidateNoText, out candidateNo);
            }

            if (!String.IsNullOrEmpty(termNoText) && !String.IsNullOrWhiteSpace(termNoText))
            {
                Int32.TryParse(termNoText, out termNo);
            }

            var objectParameters = new ObjectParameter[6];

            var candidateNoParameter = new ObjectParameter("CandidateNo", typeof(int)) { Value = candidateNo };
            objectParameters[0] = candidateNoParameter;

            var studentNameSurnameParameter = new ObjectParameter("StudentNameSurname", typeof(string)) { Value = studentNameSurname };
            objectParameters[1] = studentNameSurnameParameter;

            var coursePeriodParameter = new ObjectParameter("CoursePeriod", typeof(string)) { Value = coursePeriod };
            objectParameters[2] = coursePeriodParameter;

            var successStatusParameter = new ObjectParameter("SuccessStatus", typeof(string)) { Value = successStatus };
            objectParameters[3] = successStatusParameter;

            var teacherNameSurnameParameter = new ObjectParameter("TeacherNameSurname", typeof(string)) { Value = teacherNameSurname };
            objectParameters[4] = teacherNameSurnameParameter;

            var termNoParameter = new ObjectParameter("TermNo", typeof(int)) { Value = termNo };
            objectParameters[5] = termNoParameter;

            var list = _context.ExecuteFunction<successstatu>("GetSuccessStatusListByParameters", objectParameters).ToList();

            return list;
        }

        #endregion
    }
}
